summaryrefslogtreecommitdiffstats
path: root/src/core/hle/kernel/k_light_session.cpp
blob: d8b1e695825bb93b096b59019ef4b5fc34f6eb9a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "core/hle/kernel/k_client_port.h"
#include "core/hle/kernel/k_light_client_session.h"
#include "core/hle/kernel/k_light_server_session.h"
#include "core/hle/kernel/k_light_session.h"
#include "core/hle/kernel/k_process.h"

namespace Kernel {

KLightSession::KLightSession(KernelCore& kernel)
    : KAutoObjectWithSlabHeapAndContainer(kernel), m_server(kernel), m_client(kernel) {}
KLightSession::~KLightSession() = default;

void KLightSession::Initialize(KClientPort* client_port, uintptr_t name) {
    // Increment reference count.
    // Because reference count is one on creation, this will result
    // in a reference count of two. Thus, when both server and client are closed
    // this object will be destroyed.
    this->Open();

    // Create our sub sessions.
    KAutoObject::Create(std::addressof(m_server));
    KAutoObject::Create(std::addressof(m_client));

    // Initialize our sub sessions.
    m_server.Initialize(this);
    m_client.Initialize(this);

    // Set state and name.
    m_state = State::Normal;
    m_name = name;

    // Set our owner process.
    m_process = GetCurrentProcessPointer(m_kernel);
    m_process->Open();

    // Set our port.
    m_port = client_port;
    if (m_port != nullptr) {
        m_port->Open();
    }

    // Mark initialized.
    m_initialized = true;
}

void KLightSession::Finalize() {
    if (m_port != nullptr) {
        m_port->OnSessionFinalized();
        m_port->Close();
    }
}

void KLightSession::OnServerClosed() {
    if (m_state == State::Normal) {
        m_state = State::ServerClosed;
        m_client.OnServerClosed();
    }

    this->Close();
}

void KLightSession::OnClientClosed() {
    if (m_state == State::Normal) {
        m_state = State::ClientClosed;
        m_server.OnClientClosed();
    }

    this->Close();
}

void KLightSession::PostDestroy(uintptr_t arg) {
    // Release the session count resource the owner process holds.
    KProcess* owner = reinterpret_cast<KProcess*>(arg);
    owner->ReleaseResource(Svc::LimitableResource::SessionCountMax, 1);
    owner->Close();
}

} // namespace Kernel